window.onload=function(){
	Controller();
}


function Point(x,y){
	this.x=x;
	this.y=y;
	this.same=function(other){
		if(this.x==other.x&&this.y==other.y){
			return true;
		}else{
			return false;
		}
	}
}

function Snake(head,tail){
	this.head=head;
	this.tail=tail;
	
	
	this.move=function(dir){
		var oldDir=getDirection();
		if(dir==oldDir+2||dir==oldDir-2){
			return;	
		}else if(dir!=Direction.UP
			&&dir!=Direction.DOWN
			&&dir!=Direction.LEFT
			&&dir!=Direction.RIGHT){
				dir=oldDir;
		}
			var last=tail[tail.length-1];
			drawLast(last);
			for (var i=tail.length-1;i>=1;i--){
				tail[i].x=tail[i-1].x;
				tail[i].y=tail[i-1].y;
			}
			tail[0].x=head.x;
			tail[0].y=head.y;
			if(dir==Direction.LEFT){
				head.x=head.x-1;
			}else if(dir==Direction.UP){
				head.y=head.y-1;
			}else if(dir==Direction.RIGHT){
				head.x=head.x+1;
			}else if(dir==Direction.DOWN){
				head.y=head.y+1;
			}
			this.draw();
	}

	this.nextMove=function(dir){
		if(dir!=Direction.LEFT&&dir!=Direction.UP&&dir!=Direction.RIGHT&&dir!=Direction.DOWN)
			dir=getDirection();
		if(dir==Direction.LEFT)
			return new Point(head.x-1,head.y);
		else if(dir==Direction.UP)
			return new Point(head.x,head.y-1);
		else if(dir==Direction.RIGHT)
			return new Point(head.x+1,head.y);
		else if(dir==Direction.DOWN)
			return new Point(head.x,head.y+1);
	}
	
	this.gain=function(egg){
		var length=tail.length;
		for(var i=length;i>0;i--){
			tail[i]=tail[i-1];
			tail[i]=tail[i-1];
		}
		tail[0]=head;
		head=egg;

	}
	
	this.draw=function(){
		drawHead();
		drawTail();
	}

	function drawLast(last){
		var x=last.x;
		var y=last.y;
		var ele=document.getElementById(15*x+y);
		ele.className="box";
	}

	function getDirection(){
		var tailst=tail[0];
		if(head.x==tailst.x&&head.y==tailst.y-1){
			return Direction.UP;
		}else if(head.x==tailst.x&&head.y==tailst.y+1){
			return Direction.DOWN;
		}else if(head.x==tailst.x-1&&head.y==tailst.y){
			return Direction.LEFT;
		}else if(head.x==tailst.x+1&&head.y==tailst.y){
			return Direction.RIGHT;
		}
	}

	function drawHead(){
		drawPoint(head);
	}

	function drawTail(){
		for (var idx in tail){
			drawPoint(tail[idx]);
		}
	}

	function drawPoint(point){
		var x=point.x;
		var y=point.y;
		var ele=document.getElementById(15*x+y);
		ele.className="snake";
	}
}


function Egg(point){
	this.point=point;
	this.draw=function(){
		var x=point.x;
		var y=point.y;
		var ele=document.getElementById(15*x+y);
		ele.className="egg";
	}
}


function Board(row,column){
	this.column=column;
	this.row=row;
	this.draw=function(){
		for(var i=0;i<row;i++){
			for(var j=0;j<column;j++){
				var ele=document.createElement("div");
				ele.className="box";
				ele.id=i*column+j;
				ele.style.height=25;
				ele.style.width=25;
				ele.style.top=50+25*j;
				ele.style.left=50+25*i;
				document.getElementById("content").appendChild(ele);
			}
		}
	}
}

function Controller(){
	var column=30;
	var row=15;
	var board=new Board(column,row);
	board.draw();
	var snakeHead=new Point(Math.floor(column/2),Math.floor(row/2));
	var snakeTail=new Array(new Point(Math.floor(column/2),Math.floor(row/2)+1));
	var snake=new Snake(snakeHead,snakeTail);
	snake.draw();
	var egg=generateEgg(snake);
	egg.draw();
	window.document.onkeydown=function(e){
		var ev=e || window.event;
		var dir = ev.keyCode;
		move(dir);
	}
	var interval=setInterval(move,500);	
	function move(dir){
		var nextDir=snake.nextMove(dir);
		if(nextDir.x==egg.point.x&&nextDir.y==egg.point.y){
			snake.gain(nextDir);
			egg=generateEgg(snake);
			egg.draw();
		}else{
			if(dir!=Direction.LEFT&&dir!=Direction.UP&&dir!=Direction.RIGHT&&dir!=Direction.DOWN){
				snake.move(0);
			}else{
				snake.move(dir);
			}
		}
	}
	
}

function gameover(interval){
	alert("game over!");
}



function generateEgg(snake){
	var randX;
	var randY;
	do{
		randX=Math.floor(30*Math.random());
		randY=Math.floor(15*Math.random());
	}while(isInSnake(snake,new Point(randX,randY)));
	var egg=new Egg(new Point(randX,randY));
	return egg;
}


function isInSnake(snake,p){
	var result=false;
	var head=snake.head;
	var tail=snake.tail;
	if(head.same(p))
		result=true;
	for (var idx in tail){
		if(tail[idx].same(p))
			result=true;
	}
	return result;
}

var Direction={};
Direction.LEFT=37;
Direction.UP=38;
Direction.RIGHT=39;
Direction.DOWN=40;


